<?php

namespace App\Kuafu\Pay\Channel\WxPay\ApiV2;

/**
 * api v2版本的微信接口
 * Class WxPayApiV2
 * @package App\Kuafu\Pay\WxPay
 */
class WxPayApiV2 extends WxPayApiV2Base
{
    public function microPay(string $body, string $outTradeNo, int $totalFee, string $autoCode)
    {
        $data["body"]             = $body;
        $data["out_trade_no"]     = $outTradeNo;
        $data["total_fee"]        = $totalFee;
        $data["spbill_create_ip"] = '127.0.0.1';
        $data["auth_code"]        = $autoCode;

        return $this->wxPost('pay/micropay', $data);
    }

    public function orderQuery(string $outTradeNo)
    {
        $data = [
            'out_trade_no' => $outTradeNo,
        ];

        return $this->wxPost('pay/orderquery', $data);
    }

    /**
     * 退款
     * @param string $outTradeNo 原支付订单号
     * @param string $outRefundNo 现退款订单号
     * @param int $totalFee 原订单总金额
     * @param int $refundFee 现退款总金额
     * @return array
     */
    public function refund(string $outTradeNo, string $outRefundNo, int $totalFee, int $refundFee)
    {
        $data = [
            'out_trade_no'  => $outTradeNo,
            'out_refund_no' => $outRefundNo,
            'total_fee'     => $totalFee,
            'refund_fee'    => $refundFee,
        ];

        return $this->wxPost('secapi/pay/refund', $data);
    }

    public function refundQuery(string $out_refund_no)
    {
        $data = [
            'out_refund_no' => $out_refund_no,
        ];

        return $this->wxPost('pay/refundquery', $data);
    }

    public function downloadBill(string $date)
    {
        $data = [
            'bill_date' => $date,
            'bill_type' => 'ALL', /* ALL（默认值），返回当日所有订单信息（不含充值退款订单）
                                     SUCCESS，返回当日成功支付的订单（不含充值退款订单）
                                     REFUND，返回当日退款订单（不含充值退款订单）
                                     RECHARGE_REFUND，返回当日充值退款订单
                                   */
        ];

        return $this->wxPost('pay/downloadbill', $data);
    }

    public function close(string $outTradeNo)
    {
        $data = [
            'out_trade_no' => $outTradeNo,
        ];

        return $this->wxPost('secapi/pay/reverse', $data);
    }
}
